#include "projectdefinitions.h"
#include <avr/io.h>
#include <stdlib.h>

	
static void CreateTempArray(int a[10])     // function CreateRandomArray() definition
	{
		for (int x=0; x<10; x++)
		{
			a[x] = x+1;
		}	
	}



int main()
{


DDRA = 0b00000000;					// Port A as input (buttons)
PORTA = 0b11111111;					// Pull up resistors ON
PINA = 0b11111111;					// No button pressed
DDRC = 0b11111111;					// Port C as output (LEDs)
PORTC = 0b11111111;					// Switch off LEDs



//uint8_t sum = 0;                    // Declaring and initializing variables
//uint8_t count = 0;

int timer = 0;
int maintimer = 0;
int Temp[10];

CreateTempArray(Temp);

int imax=9;
int i;

i=imax;

while(1)
{

	if(!(PINA & (1<<3)) | !(PINA & (1<<4)) | !(PINA & (1<<5)) | !(PINA & (1<<6)) | !(PINA & (1<<7)))
	{
		while(!(PINA & (1<<3)) | !(PINA & (1<<4)) | !(PINA & (1<<5)) | !(PINA & (1<<6)) | !(PINA & (1<<7)))
		;


		ACTIVATE_LCD;
		CLEAR_LCD
		maintimer = 0;


		while(maintimer<1000)
		{

			LCD_XY(6, 3)
			LCD_D_NUMBER(Temp[i],2,0)	

			if(!(PINA & (1 << 4))) 		 // Check if DOWN is pressed
			{	
				if(i<9)
				{
					i=i+1;
				}

				double longpress = 0;
				while(!(PINA & (1 << 4)))
				{
					while(!(PINA & (1 << 4)) && longpress<800)
					{
						longpress++;
					}
					if(longpress == 800 && i<9)
					{
						double scrolltime=0;
						while(scrolltime<100 && !(PINA & (1 << 4)))
						{	
							scrolltime++;
						}
						CLEAR_LCD
						i=i+1;
						LCD_XY(6, 3)
						LCD_D_NUMBER(Temp[i],2,0)	
					}
				}

				maintimer = 0;
				CLEAR_LCD
			}
			


			if(!(PINA & (1 << 6))) 		 // Check if UP is pressed
			{
				if(i>0)
				{
					i=i-1;
				}

				double longpress = 0;
				while(!(PINA & (1 << 6)))
				{
					while(!(PINA & (1 << 6)) && longpress<800)
					{
						longpress++;
					}
					if(longpress == 800 && i>0)
					{
						double scrolltime=0;
						while(scrolltime<100 && !(PINA & (1 << 6)))
						{	
							scrolltime++;
						}
						CLEAR_LCD
						i=i-1;
						LCD_XY(6, 3)
						LCD_D_NUMBER(Temp[i],2,0)	
					}
				}

				maintimer = 0;
				CLEAR_LCD
			}

			if(!(PINA & (1 << 5))) 		 // Check if RIGHT is pressed
			{
				while(!(PINA & (1 << 5)))
				;

				maintimer = 0;

				CLEAR_LCD

				timer = 0;

				while(timer<400)
				{
					LCD_XY(13, 3)
					LCD_TEXT("Max")
					timer++;
					maintimer++;

					if(!(PINA & (1 << 7)))
					{
						while(!(PINA & (1 << 7)))
						;
						timer = 400;
						maintimer = 0;
						CLEAR_LCD
					}

					if(!(PINA & (1 << 5))) 		 
					{
						while(!(PINA & (1 << 5)))
						;
						timer = 0;
						maintimer = 0;
					}

					if(timer==400)
					{
						CLEAR_LCD
					}

					if(!(PINA & (1 << 3))) 		 // Check if CENTER is pressed
					{
						while(!(PINA & (1 << 3)))
						;
						timer = 400;
						maintimer = 1000;
					}
				}
		
			}

		
			if(!(PINA & (1 << 7))) 		 // Check if left is pressed
			{
				while(!(PINA & (1 << 7)))
				;

				maintimer = 0;

				CLEAR_LCD

				timer = 0;
				while(timer<400)
				{
					LCD_XY(1, 3)
					LCD_TEXT("Min")
					timer++;
					maintimer++;

					if(!(PINA & (1 << 5)))
					{
						while(!(PINA & (1 << 5)))
						;
						timer = 400;
						maintimer = 0;
						CLEAR_LCD
					}

					if(!(PINA & (1 << 7))) 		 
					{
						while(!(PINA & (1 << 7)))
						;
						timer = 0;
						maintimer = 0;
					}

					if(timer==400)
					{
						CLEAR_LCD
					}

					if(!(PINA & (1 << 3))) 		 // Check if CENTER is pressed
					{
						while(!(PINA & (1 << 3)))
						;
						timer = 400;
						maintimer = 1000;
					}
				}

			}

			if(!(PINA & (1 << 3))) 		 // Check if CENTER is pressed
			{
				while(!(PINA & (1 << 3)))
				;

				maintimer = 1000;
			}

			maintimer++;
		}
 
 		if(maintimer>=1000)
		{
			CLEAR_LCD
			LCD_BACKLIGHT_OFF
			i=imax;
		}
	}
}
return 0;}

